<template>
  <div class="app-container">
        医院设置添加
        <el-form label-width="120px">
           <el-form-item label = "医院名称">
              <el-input v-model="hospitalSet.hosname"/>
           </el-form-item>
           <el-form-item label="医院编号">
              <el-input v-model="hospitalSet.hoscode"/>
           </el-form-item>
           <el-form-item label="api基础路径">
              <el-input v-model="hospitalSet.apiUrl"/>
           </el-form-item>
           <el-form-item label="联系人姓名">
              <el-input v-model="hospitalSet.contactsName"/>
           </el-form-item>
           <el-form-item label="联系人手机">
              <el-input v-model="hospitalSet.contactsPhone"/>
           </el-form-item>
           <el-form-item>
              <el-button type="primary"@click="saveOrUpdate()">保存</el-button>
           </el-form-item>
        </el-form>
     </div>
</template>

<script>
    import hospApi from '@/api/hospset'
    export default {
        data(){
            return{
                hospitalSet:{}
            }
        },
        created(){
          // 获取路由前的信息获取id值
            if (this.$route.params&&this.$route.params.id){
                const  id=this.$route.params.id
                console.log(id)
                this.getById(id)
            }else {
                this.hospitalSet={}
            }
        },
        mounted(){

        },
        methods:{
            // g根据id查询
            getById(id){
              hospApi.getById(id)
                  .then(res=>{
                      this.hospitalSet=res.data
                  })
            },
            // 添加
            saveOrUpdate(){
                console.log(this.hospitalSet)
                if (!this.hospitalSet.id){
                    // 没有id添加
                    hospApi.saveHospSet(this.hospitalSet)
                        .then(res=>{
                            alert(res)
                            // 路由跳转
                            this.$router.push({path:'/hospital/list'})
                        })
                        .catch(error=>{

                        })
                }else {
                    // 修改
                    hospApi.updateHospSet(this.hospitalSet)
                        .then(res=>{
                            alert(res)
                            // 路由跳转
                            this.$router.push({path:'/hospital/list'})
                        })
                        .catch(error=>{

                        })
                }
            }
        }
    }
</script>

<style scoped>

</style>
